package com.search.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class PropertyUtil {
	public static String getProperty(String field) {
		Properties p = new Properties();
		try {
			p.load(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("system.properties"), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p.getProperty(field);
	}

	public void setProperty(String field, String value) {
		Properties p = new Properties();
		try {
			p.load(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("system.properties"), "UTF-8"));
			OutputStream fos = new FileOutputStream(getClass().getResource("/system.properties").getPath());
			p.setProperty(field, value);
			p.store(fos, "Update '" + field + "' value");
			System.out.println("field:" + field);
			System.out.println("value:" + value);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
		}
	}
}
